var db = require('../dbutil/index');
var sql = require('../dbutil/sql');

module.exports = {
    // 添加订单
    addOrder: (req, res) => {
        //从请求体中获取对象
        const { ordernum, uid, ordertime, orderstatus, ordertotal, pricestatus,sendoutGoods,ReceivingStatus } = req.body;
        db.query(sql.accounts.addOrder, [ordernum, uid, ordertime, orderstatus, ordertotal, pricestatus, sendoutGoods,ReceivingStatus], (err, results) => {
            if (err) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                // console.log(results);
                res.send({ code: 200, message: 'ok', data: { orderid: results.insertId } });
            }
        })
    },
    // 添加订单详情
    addOrderDetail: (req, res) => {
    //从请求体中获取对象
    const { orderid, gid, num,addressid } = req.body;
    db.query(sql.accounts.addOrderDetail, [orderid, gid, num, addressid], (err, results) => {
        if (err) {
            res.send({ code: 500, message: 'SQL执行错误' });
        } else {
            res.send({ code: 200, message: 'ok' });
        }
    });
},
    priceStatus: (req, res) => {
        let priceStatus = req.body.priceStatus;
        console.log(req.body);
        db.query(sql.accounts.priceStatus, [priceStatus.pricestatus, priceStatus.orderid], (err3, results3) => {
            if (err3) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok' });
            }
        })
    },
    addRess: (req, res) => {
        let myaddress = req.body.myaddress;
        console.log(myaddress);
        db.query(sql.accounts.addRess, [myaddress.uid, myaddress.name, myaddress.phone, myaddress.address], (err3, results3) => {
            if (err3) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok'}); 
            }
        })
    },
    sele: (req, res) => {
        let sel = req.body.sel;
        // console.log('body:'+req.body); 
        db.query(sql.accounts.sele, [sel.uid], (err4, results4) => {
            if (err4) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data: { results4 } });
            }
        })
    },
    addid: (req, res) => {
        let addid2 = req.body.addid2;
        // console.log('body:'+req.body); 
        db.query(sql.accounts.addid, [addid2.uid], (err5, results5) => {
            if (err5) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok', data: { results5 } });
            }
        })
    },
    adddel: (req, res) => {
        let addressid = req.body.addressid;
        //   console.log('Received addressid:', req.body.addressid); // 打印接收到的 addressid
        db.query(sql.accounts.adddel, [addressid], (err, results) => {
            if (err) {
                res.send({ code: 500, message: 'sql执行错误' });
            } else {
                res.send({ code: 200, message: 'ok' })
            }
        })
    },
    ReceivingStatus:(req,res)=>{ //收货
            let orderid = req.body.orderid;
            db.query(sql.accounts.ReceivingStatus, [orderid, orderid], (err, results) => {
                if (err) {
                    // 如果发生错误，回滚事务
                    return res.send({ code: 500, message: 'SQL执行错误' });
                }
                res.send({ code: 200, message: '商品已收货' });
            });
    
        },

}